package cn.itcast.wanxinp2p.account.service.impl;

import cn.itcast.common.RestResponse;
import cn.itcast.exception.BusinessException;
import cn.itcast.wanxinp2p.account.common.AccountErrorCode;
import cn.itcast.wanxinp2p.account.entity.Account;
import cn.itcast.wanxinp2p.account.mapper.AccountMapper;
import cn.itcast.wanxinp2p.account.model.AccountDTO;
import cn.itcast.wanxinp2p.account.model.AccountLoginDTO;
import cn.itcast.wanxinp2p.account.model.AccountRegisterDTO;
import cn.itcast.wanxinp2p.account.service.AccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private AccountMapper accountMapper;

    @Value("${sms.enable}")
    private String smsEnable;

    @Override
    public RestResponse getSMSCode(String mobile) {
        //发起远程调用  调用短信微服务发送短信   RestTemplate

        //远程调用url地址
        String url = "http://localhost:56085/sailing/generate?effectiveTime=300&name=sms";

        HashMap<String, String> map = new HashMap<>();
        map.put("mobile", mobile);
        //获取验证码
        RestResponse restResponse = restTemplate.postForObject(url, map, RestResponse.class);
        return restResponse;
    }

    @Override
    public Integer checkMobile(String mobile, String key, String code) {
        //restTemplate远程调用
        String url = "http://localhost:56085/sailing/verify?name=sms&verificationKey=" + key + "&verificationCode=" + code;
        //获取查询返回值
        RestResponse restResponse = restTemplate.postForObject(url, null, RestResponse.class);
        if (!(Boolean) restResponse.getResult()) {
            throw new BusinessException(String.valueOf(AccountErrorCode.E_140152));
        }
        return accountMapper.getMobile(mobile);
    }

    @Override
    public AccountDTO register(AccountRegisterDTO registerDTO) {
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setMobile(registerDTO.getMobile());
        accountDTO.setUsername(registerDTO.getUsername());
        accountDTO.setStatus(1);
        accountDTO.setDomain("c");
        accountMapper.register(registerDTO);

        Account account = accountMapper.getByName(accountDTO.getUsername());
        Long id = account.getId();
        accountDTO.setId(id);

        return accountDTO;
    }

    @Override
    public AccountDTO login(AccountLoginDTO accountLoginDTO) {
        // 1、查询用户，注意此处需要根据前端传递的 AccountLoginDTO的domain做判断：
        //  如果值是 c 表示是c端用户，需要根据手机号查询
        //  否则是b端用户，需要根据用户名查询
        int accountCount = accountMapper.getMobile(accountLoginDTO.getMobile());

        Account account = accountMapper.getByMobile(accountLoginDTO.getMobile());

        //2、判断用户是否存在，如果不存在，直接抛出 AccountErrorCode.E_130104 异常
        if (accountCount == 0) {
            throw new BusinessException(String.valueOf(AccountErrorCode.E_130104));
        }

        AccountDTO accountDTO = new AccountDTO();
        BeanUtils.copyProperties(account, accountDTO);

        //3、如果 smsEnable=true ，说明是短信验证码登录，不做密码校验。直接返回
        if ("true".equals(smsEnable)) {
            return accountDTO;
        }
        //4、验证密码，如果密码错误则抛出 AccountErrorCode.E_130105 异常
        if (!account.getPassword().equals(accountLoginDTO.getPassword())) {
            throw new BusinessException(String.valueOf(AccountErrorCode.E_130105));
        }

        return accountDTO;
    }

    //confirm
    public void confirmRegister(AccountRegisterDTO registerDTO) {
        System.out.println("execute confirmRegister");
    }

    //cancel
    public void cancelRegister(AccountRegisterDTO registerDTO) {
        System.out.println("execute cancelRegister");
        //TODO 删除账号
        accountMapper.deleteByMobile(registerDTO.getMobile());
    }
}
